package com.ying.controller.system;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ying.config.MinioPropertiesConfig;
import com.ying.core.response.ApiResponse;
import com.ying.core.page.PageWrap;
import com.ying.core.response.ResultCode;
import com.ying.core.utils.MinioUtil;
import com.ying.core.utils.WordFilterUtil;
import com.ying.po.system.User;
import com.ying.service.system.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 示例Controller
 *
 * @author 硬
 * @date 2021/12/04 20:13
 */
@RestController
@RequestMapping("/api/user")
@Api(tags = "User接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private WordFilterUtil wordFilterUtil;

    @Autowired
    private MinioUtil minioUtil;

    @Autowired
    private MinioPropertiesConfig minioPropertiesConfig;

    /**
     * 创建
     *
     * @author 硬
     * @date 2021/12/04 20:13
     */
    @SaCheckPermission("user-add")
    @PostMapping("/create")
    @ApiOperation("创建用户")
    public ApiResponse create(@RequestBody User user) {

        // 存储user
        Integer id = userService.create(user);
        return ApiResponse.success(id);
    }

    /**
     * 通过id查询
     *
     * @author 硬
     * @date 2021/12/04 20:13
     */
    @SaCheckPermission("user-get")
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询")
    public ApiResponse finById(@PathVariable(value = "id") Integer id) {
        return ApiResponse.success(userService.findById(id));

    }

    @SaCheckPermission("user-get")
    @GetMapping("/find/roleCode")
    @ApiOperation("根据角色ID查询")
    public ApiResponse finByRoleId(@RequestParam("roleCode") Integer roleCode) {
        List<User> userList = userService.findByRoleCode(roleCode);
        return ApiResponse.success(userList);
    }

    /**
     * 分页查询
     *
     * @author Caesar Liu
     * @date 2020-06-27 14:31
     */
    @SaCheckPermission("user-get")
    @PostMapping("/page")
    @ApiOperation("分页查询")
    public ApiResponse findPage(@RequestBody PageWrap<User> pageWrap) {
        return ApiResponse.success(userService.findAllByPage(pageWrap));
    }

    /**
     * 根据ID修改
     *
     * @author 硬
     * @date 2021/12/04 20:13
     */
    @SaCheckPermission("user-update")
    @PutMapping("/update")
    @ApiOperation("修改用户")
    public ApiResponse updateById(@RequestBody User user) {
        userService.updateById(user);
        return ApiResponse.success(null);
    }

    /**
     * 根据ID删除
     *
     * @author 硬
     * @date 2021/12/04 20:13
     */
    @SaCheckPermission("user-delete")
    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据ID删除")
    public ApiResponse delete(@PathVariable Integer id) {
        userService.deleteById(id);
        return ApiResponse.success(null);
    }

    @GetMapping("/countall")
    @SaCheckPermission("user-get")
    @ApiOperation("查询用户数量")
    public ApiResponse countAll() {
        long count = userService.countAll();
        return ApiResponse.success(count);
    }

    @SaCheckPermission("user-get")
    @PostMapping("/findbyids")
    @ApiOperation("根据ID列表查询用户")
    public ApiResponse findByIds(@RequestBody Integer[] ids) {
        List<User> userList = userService.findByIdInBatch(Arrays.asList(ids));
        return ApiResponse.success(userList);
    }

    @GetMapping("/find/all")
    @SaCheckPermission("user-get")
    @ApiOperation("查询全部的用户")
    public ApiResponse findAll() {
        ApiResponse success = ApiResponse.success(userService.findAll());
        return success;
    }

    @PostMapping("/query")
    @SaCheckPermission("user-get")
    @ApiOperation("根据条件查询用户")
    public ApiResponse queryAllByPage(@RequestBody PageWrap<User> pageWrap) {
        return ApiResponse.success(userService.queryAllByPage(pageWrap));
    }

    @PostMapping("/sensitiveWord")
    @SaCheckPermission("user-check")
    @ApiOperation("User对象敏感词检查")
    public ApiResponse checkSensitiveWord(@RequestBody User user) {
        boolean s = userService.checkSensitiveWord(user);
//        boolean s = false;
        if (s)
            return ApiResponse.failure(ResultCode.DATA_IS_SENSITIVE);
        return ApiResponse.success();
    }

    @ApiOperation("上传头像")
    @PostMapping("/uploadAvatar")
    @SaCheckPermission("user-upload")
    public ApiResponse updateAvatar(@RequestBody MultipartFile multipartFile, @RequestParam("userid") Integer userid) {
        String filepath = minioPropertiesConfig.getAvatarPath();
        String avatarPath = minioUtil.upload(multipartFile, filepath);
        String endpoint = minioPropertiesConfig.getEndpoint();
        String bucket = minioPropertiesConfig.getBucketName();
        String basePath = minioPropertiesConfig.getBasePath();
        if (avatarPath.startsWith("/")) {
            avatarPath = basePath + "/" + bucket + avatarPath;
        } else {
            avatarPath = basePath + "/" + bucket + "/" + avatarPath;
        }
        User user = userService.findById(userid);
        if (user != null) {
            user.setAvatar(avatarPath);
            userService.updateById(user);
            return ApiResponse.success(avatarPath);
        } else {
            return ApiResponse.failure(ResultCode.RESULE_DATA_NONE);
        }

    }

}
